﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using System.Reflection;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Client;
using Cn.Edwon.Web;
using Cn.Cellula.Model.Oms;
using Cn.Cellula.Biz.Port;

namespace Cn.Cellula.Website.Admin.Oms.ShippingAddress {
    public partial class Edit : PageSupport {
        String EOKey = null;
        IBiz biz = BizFactory.Get<IBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm(EOKey);
                }
            }
        }
        private void FillForm(String key) {
            TOmsShippingAddressEO eo = biz.Load<TOmsShippingAddressEO>(key);
            this.txt_ValidTimeDesc.Text = eo.ValidTimeDesc;
            this.txt_Tel.Text = eo.Tel;
            this.txt_SortOrder.Text = eo.SortOrder.ToString();
            this.txt_ReciverName.Text = eo.ReceiverName;
            this.txt_Mobile.Text = eo.Mobile;
            this.txt_Location.Text = eo.Location;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_Location.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            if (String.IsNullOrEmpty(EOKey)) {
                TOmsShippingAddressEO eo = new TOmsShippingAddressEO();
                eo.CreateDate = eo.LastDate = DateTime.Now;
                eo.Location = this.txt_Location.Text;
                eo.Mobile = this.txt_Mobile.Text;
                eo.ReceiverName = this.txt_ReciverName.Text;
                try {
                    eo.SortOrder = Convert.ToInt32(this.txt_SortOrder.Text);
                } catch {
                    eo.SortOrder = 0;
                }
                eo.Tel = this.txt_Tel.Text;
                eo.ValidTimeDesc = this.txt_ValidTimeDesc.Text;
                eo.OwnerKey = this.OwnerKey;
                biz.Save<TOmsShippingAddressEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("收获地址", eo.ReceiverName, this.Actor, Request);
            } else {
                TOmsShippingAddressEO eo = biz.Load<TOmsShippingAddressEO>(EOKey);
                eo.LastDate = DateTime.Now;
                eo.Location = this.txt_Location.Text;
                eo.Mobile = this.txt_Mobile.Text;
                eo.ReceiverName = this.txt_ReciverName.Text;
                try {
                    eo.SortOrder = Convert.ToInt32(this.txt_SortOrder.Text);
                } catch {
                    eo.SortOrder = 0;
                }
                eo.Tel = this.txt_Tel.Text;
                eo.ValidTimeDesc = this.txt_ValidTimeDesc.Text;
                biz.Update<TOmsShippingAddressEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("收获地址", eo.ReceiverName, this.Actor, Request);
            }
            Response.Redirect("List.aspx");
        }
    }
}
